function getCookie(c_name) {
    if (document.cookie.length>0){
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1){
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
      } 
  	}
	return "";
}
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/jpw";
}
function clearCookie(name) {  
    setCookie(name, "", -1);  
}
function clearCookies(names) {
	for (var i = 0; i<names.length; i++) {
		setCookie(names[i], "", -1);
	}
}
$(function() {
	var ck = new Array("lAlias", "lName", "lPass");
	
	var lAlias = getCookie("lAlias");
	var lName = getCookie("lName");
	var lPass = getCookie("lPass");
	
	var full = lAlias != "" && lAlias != null && lName != "" && lName != null && lPass != "" && lPass != null;
	if (full) {
		//alert("full: " + full);
		//alert("lAlias: " + lAlias + " lName: " + lName + " lPass: " + lPass);
		$.ajax({
			cache: false,
			url:"log/keepLogin",
			type: "GET",
			data: {lalias: lAlias, luser: lName, lpass: lPass},
			dataType:"json",
		    success: function(data) {
		    	//alert(data.msg + "--" + data.lalias + "--" + data.luser + "--" + data.lpass);
		    	if (data.msg == "success") {
		    		setCookie('lAlias',data.lalias,1);
    		    	setCookie('lName',data.luser,1);
    		    	setCookie('lPass',data.lpass,1);
					$("#top_login").empty();
					$("#top_login").append("<a href='selfCenter'>" + data.lalias + "</a>" + "&nbsp;&nbsp" + "<a href='JavaScript:void(0);' id='user_layout'>退出</a>");
					$("#my_jpw").attr("href", "selfCenter");	
		    	} else {
		    		$("#top_login").empty();
					$("#top_login").append("<a href='user/login.jsp'>请登录</a>");
					clearCookies(ck);
		    	}
		    },
			error: function() {
				$("#top_login").empty();
				$("#top_login").append("<a href='user/login.jsp'>请登录</a>");
				clearCookies(ck);
			}
		});
	} else {
		$("#top_login").empty();
		$("#top_login").append("<a href='login.html'>请登录</a>"+ "&nbsp;&nbsp" +"<a href='user/reg/register.jsp'>注册</a>");
		$("#my_jpw").attr("href", "user/login.jsp");
		clearCookies(ck);
	}
	
	$("#user_layout").live("click",function(){
		$.ajax({
			cache: false,
			url:"log/logout",
			type: "GET",
			data: {lalias: lAlias, luser: lName, lpass: lPass},
			dataType:"json",
		    success: function(data) {
		    	if (data == "success") {
		    		$("#top_login").empty();
		    		$("#top_login").append("<a href='login.html'>请登录</a>"+ "&nbsp;&nbsp" +"<a href='user/reg/register.jsp'>注册</a>");
		    		$("#my_jpw").attr("href", "user/login.jsp");
		    		clearCookies(ck);
		    	}
		    },
		});
	});
	
});